package com.iamlbccc.opengles;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.Toast;

import com.iamlbccc.opengles.renders.FirstOpenGLProjectRenderer;

public class MainActivity extends AppCompatActivity {
    private GLSurfaceView mGlSurfaceView;
    private FirstOpenGLProjectRenderer mRenderer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGlSurfaceView = findViewById(R.id.gl_surface_view);

        if (isEs2Supported()) {
            mRenderer = new FirstOpenGLProjectRenderer();
            mGlSurfaceView.setEGLContextClientVersion(2);
            mGlSurfaceView.setRenderer(mRenderer);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mRenderer != null) {
            mGlSurfaceView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mRenderer != null) {
            mGlSurfaceView.onPause();
        }
    }

    private boolean isEs2Supported() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
        boolean supported = configurationInfo.reqGlEsVersion >= 0x20000;
        if (!supported) {
            Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
                    Toast.LENGTH_LONG).show();
        }
        return supported;
    }
}
